
var SEP = "增值税专用发票";
var MAN = "通用手工发票";
var FIX = "定额发票";
var OTH = "其它发票";

//查找上期未抵减税额
function left()
{
	var action = new Action("GetPreviousLeftCredit");
	action.identify = identify.value;
	action.year = year.value;
	action.month = month.value;
	
	var callback = function(result)
	{
		preLeftCredit.value = result.money;
		money();
	}
	
	Rpc.call(action, callback);
}

//检查金额填写的正确性
function check(box)
{
	var v = box.value;
	if (!v) v = 0;
	if (!isDoubleFormat(v))
	{
		alert("请输入正确的金额！");
		return;
	}
	
	money();
}

//计算累计金额
function money()
{
	var sepInvoiceMoney = 0;
	for (var i = 0; i < sepInvoiceWidgets.length; i++)
	{
		var v = sepInvoiceWidgets[i].money.value;
		if (!v) v = 0;
		sepInvoiceMoney += parseFloat(v);
	}
	
	var manInvoiceMoney = 0;
	for (var i = 0; i < manInvoiceWidgets.length; i++)
	{
		var v = manInvoiceWidgets[i].money.value;
		if (!v) v = 0;
		manInvoiceMoney += parseFloat(v);
	}
	
	var fixInvoiceMoney = 0;
	for (var i = 0; i < fixInvoiceWidgets.length; i++)
	{
		var v = fixInvoiceWidgets[i].money.value;
		if (!v) v = 0;
		fixInvoiceMoney += parseFloat(v);
	}
	
	var othInvoiceMoney = 0;
	for (var i = 0; i < othInvoiceWidgets.length; i++)
	{
		var v = othInvoiceWidgets[i].money.value;
		if (!v) v = 0;
		othInvoiceMoney += parseFloat(v);
	}
	
	//
	var allow = 0;
	allow += sepInvoiceMoney;// / 1.17;
	allow += manInvoiceMoney;
	allow += fixInvoiceMoney;
	allow += othInvoiceMoney;
	
	thisAllowCredit.value = Math.floor(allow * 100)/100;
	
	var pre = parseFloat(preLeftCredit.value);
	var total = allow + pre;
	totalAllowCredit.value = Math.floor(total * 100)/100;
	
	var apply = applyVatCredit.value;
	if (apply) total -= parseFloat(apply);
	thisLeftCredit.value = Math.floor(total * 100)/100;
}

function submit(button, close)
{
	if (!validate()) return;
	
	var getInvoices = function(group, category)
	{
		var invoices = [];
		
		for (var i = 0; i < group.length; i++)
		{
			var invoice = {};
			
			invoice.invoiceCategory = category;
			if (group[i].code) invoice.invoiceCode = group[i].code.value;
			if (group[i].num) invoice.invoiceNum = group[i].num.value;
			invoice.invoiceTypeId = group[i].type.value;
			invoice.invoiceMoney = group[i].money.value;
			invoice.invoiceDate = parseDate(group[i].date.value);
			
			invoices.push(invoice);
		}
		
		return invoices;
	}
	
	var action = new Action("SubmitVatCredit");
	action.id= $("credit_id").value;
	action.year = year.value;
	action.month = month.value;
	action.identify = identify.value;
	
	var allInvoices = [];
	allInvoices = allInvoices.concat(getInvoices(sepInvoiceWidgets, SEP));
	allInvoices = allInvoices.concat(getInvoices(manInvoiceWidgets, MAN));
	allInvoices = allInvoices.concat(getInvoices(fixInvoiceWidgets, FIX));
	allInvoices = allInvoices.concat(getInvoices(othInvoiceWidgets, OTH));
	
	action.invoices = allInvoices;
	action.applyVatCredit = applyVatCredit.value;
	
	var callback = function(result)
	{
		alert("提交成功，表格将重置。您可以在提交历史查询中查找刚提交的抵减清单。");
		if (close) window.close();
		else location.reload();
	}
	var error = function(message)
	{
		alert(message);
		button.disabled = false;
		button.value = "提交"
	}
	Rpc.call(action, callback, error);
	
	button.disabled = true;
	button.value = "提交中";
}

function validate()
{
	if (!identify.value)
	{
		alert("请填写纳税人识别号！")
		identify.focus();
		return false;
	}
	
	if (!validateInvoices(sepInvoiceWidgets))
	{
		return false;
	}
	if (!validateInvoices(manInvoiceWidgets))
	{
		return false;
	}
	if (!validateInvoices(fixInvoiceWidgets))
	{
		return false;
	}
	if (!validateInvoices(othInvoiceWidgets))
	{
		return false;
	}
	
	if (!applyVatCredit.value)
	{
		alert("请填写本期申报抵减税额！")
		applyVatCredit.focus();
		return false;
	}
	if (!isDoubleFormat(applyVatCredit.value))
	{
		alert("请填写正确的本期申报抵减税额！")
		applyVatCredit.focus();
		return false;
	}
	
	return true;
}

function validateInvoices(group)
{
	for (var i = 0; i < group.length; i++)
	{
		var widgets = group[i];
		if (widgets.type.value < 0) continue;
		
		if (widgets.code && !widgets.code.value)
		{
			alert("请填写发票代码！");
			widgets.code.focus();
			return false;
		}
		if (widgets.num && !widgets.num.value)
		{
			alert("请填写发票号码！");
			widgets.num.focus();
			return false;
		}
		if (!widgets.money.value)
		{
			alert("请填写价税合计金额！");
			widgets.money.focus();
			return false;
		}
		if (!isDoubleFormat(widgets.money.value))
		{
			alert("请填写正确的价税合计金额！");
			widgets.money.focus();
			return false;
		}
		if (!widgets.date.value)
		{
			alert("请填写日期！");
			widgets.date.focus();
			return false;
		}
		if (!isShortDateFormat(widgets.date.value))
		{
			alert("请填写正确的日期（日期格式为：20120101）！");
			widgets.date.focus();
			return false;
		}
	}
	return true;
}